package com.zx.DataStructuresAndAlgorithms;

import com.zx.DataStructuresAndAlgorithms.dao.RandTest;

import static com.zx.DataStructuresAndAlgorithms.dao.SortSwap.swapInsert;


/**
 * @Author lovexu
 * @Date 2022/10/22 14:22
 * @ClassName InsertionSort
 * @Description TODO
 **/
public class InsertionSort {
public static <AnyType extends  Comparable<?super AnyType>>
void insertionUp(AnyType[]arr) {
    int j;
    for (int i = 1; i <arr.length; i++) {
        AnyType temp=arr[i];
        for (j=i;j>0 && arr[j-1].compareTo(temp)>0;j--){
            arr[j]=arr[j-1];
        }
        arr[j]=temp;
    }
}
    public static <AnyType extends  Comparable<?super AnyType>>
    void insertionDown(AnyType[]arr) {
        int j;
        for (int i = 1; i <arr.length; i++) {
            AnyType temp=arr[i];
            for (j=i;j>0 && arr[j-1].compareTo(temp)<0;j--){
                swapInsert(arr, j, arr[j-1]);
            }
            swapInsert(arr, j, temp);
        }
    }



    public static <AnyType extends Comparable<? super AnyType>>
    void outPut(AnyType[] arr) {
        insertionUp(arr);
        System.out.print("从小到大排序:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+",");
        }
        System.out.println();
        System.out.print("从大到小排序:");
        insertionDown(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+",");
        }
        System.out.println();
    }
    public static void main(String[] args) {
//        Integer[] arr1 = {6, 3, 4, 5, 7, 8, 8, 9};
//        SysInfoSupply.sysInfo(arr1);
//        outPut(arr1);

        //-------------------
        //随机生成测试用例
        Integer []arr2=new Integer[8];
        RandTest.randGenerateInteger(arr2);
        outPut(arr2);
    }
}
